package com.swotplus.admin.web.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swotplus.framework.web.BaseController;
import com.swotplus.common.annotation.Log;
import com.swotplus.common.core.AjaxResult;
import com.swotplus.common.enums.BusinessType;
import com.swotplus.common.utils.StringUtils;
import com.swotplus.framework.service.SysPasswordService;
import com.swotplus.system.sys.entity.SysLogininfor;
import com.swotplus.system.sys.service.SysLogininforService;
import com.swotplus.system.sys.vo.SysLogininforVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 系统访问记录
 * 
 * @author hugang
 */
@Controller
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController
{
    private String prefix = "monitor/logininfor";

    @Autowired
    private SysLogininforService sysLogininforService;

    @Autowired
    private SysPasswordService sysPasswordService;

    @RequiresPermissions("monitor:logininfor:view")
    @GetMapping()
    public String logininfor()
    {
        return prefix + "/list";
    }

    @RequiresPermissions("monitor:logininfor:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(SysLogininforVo vo)
    {
        Page<SysLogininfor> page=new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<SysLogininfor> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(vo.getLoginName())){
            queryWrapper.eq("login_name",vo.getLoginName());
        }
        queryWrapper.orderByDesc("login_time");
        IPage<SysLogininfor> pageList= sysLogininforService.page(page,queryWrapper);
        return AjaxResult.success("成功", (int) pageList.getTotal(),pageList.getRecords());
    }


    @RequiresPermissions("monitor:logininfor:remove")
    @Log(title = "登陆日志", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysLogininforService.removeById(ids));
    }
    
    @RequiresPermissions("monitor:logininfor:remove")
    @Log(title = "登陆日志", businessType = BusinessType.CLEAN)
    @PostMapping("/clean")
    @ResponseBody
    public AjaxResult clean()
    {
        sysLogininforService.cleanLogininfor();
        return success();
    }

    @RequiresPermissions("monitor:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @PostMapping("/unlock")
    @ResponseBody
    public AjaxResult unlock(String password)
    {
        sysPasswordService.unlock(password);
        return success();
    }
}
